package com.ygr.anime.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.ygr.anime.domain.User;
import com.ygr.anime.dto.UserDto;
import com.ygr.anime.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SaCheckLogin
@RestController
@RequestMapping("/user")
class UserController {
    private final UserService userService;

    UserController(UserService userService) {
        this.userService = userService;
    }

    @SaCheckRole("admin")
    @GetMapping("/list")
    public SaResult list() {
        return SaResult.data(userService.list());
    }

    @GetMapping("/{id}")
    public SaResult get(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        return SaResult.ok("获取用户信息成功").setData(
                new UserDto(user.getId(),
                        user.getName(),
                        user.getNickname(),
                        user.getAvatar(),
                        StpUtil.getRoleList()
                )
        );
    }
}
